package com.adderis.soft.domain.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.adderis.soft.domain.dao.INomcommercialDao;
import com.adderis.soft.domain.model.NomCommercial;

@Repository("nomcommercialDao")
public class NomcommercialDaoImpl implements INomcommercialDao {

	private static final Log log = LogFactory
			.getLog(NomcommercialDaoImpl.class);

	Calendar calendar = Calendar.getInstance();
	java.util.Date now = calendar.getTime();
	Timestamp nowtimesstamp = new java.sql.Timestamp(now.getTime());;

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * {@inheritDoc}
	 */
	public void persist(NomCommercial transientNomcommercial) {
		log.debug("Persisting NomCommerial instance");
		try {
			transientNomcommercial.setCreatetimestamp(nowtimesstamp);
			transientNomcommercial.setStatutcode("A");
			entityManager.persist(transientNomcommercial);

			log.debug("persist successful");

		} catch (RuntimeException re) {
			log.error("persist failed", re);
		}

	}

	/**
	 * {@inheritDoc}
	 */

	public void remove(NomCommercial persistentNomcommercial) {
		log.debug("removing NomCommercial instance");
		try {
			persistentNomcommercial.setStatutcode("D");
			persistentNomcommercial.setDeletiontimesstamp(nowtimesstamp);
			entityManager.merge(persistentNomcommercial);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.debug("remove failed", re);
		}

	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(Integer NomcommercialId) {
		this.remove(this.findById(NomcommercialId));

	}

	/**
	 * {@inheritDoc}
	 */
	public NomCommercial merge(NomCommercial detachedNomcommercial) {
		log.debug("merging nomCommercial instance");
		try {
			detachedNomcommercial.setUpdatetimesstamp(nowtimesstamp);
			NomCommercial result = entityManager.merge(detachedNomcommercial);
			entityManager.merge(detachedNomcommercial);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public NomCommercial findById(Integer id) {
		log.debug("getting nomCommercial instance with id: " + id);
		try {
			NomCommercial instance = entityManager
					.find(NomCommercial.class, id);
			log.debug("findById successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("findById failed", re);
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */

	@SuppressWarnings("unchecked")
	public List<NomCommercial> findAll() {
		log.debug("getting all nomCommercial instance");
		try {
			Query query = entityManager
					.createQuery("SELECT c FROM NomCommercial c");
			List<NomCommercial> nomCommercialList = (List<NomCommercial>) query
					.getResultList();
			log.debug("findAll successful");
			return nomCommercialList;

		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			return new ArrayList<NomCommercial>();
		}

	}

	/**
	 * {@inheritDoc}
	 */

	@SuppressWarnings("unchecked")
	public List<NomCommercial> search(String searchString) {
		log.debug("Search nomCommercial instances with search string: "
				+ searchString);
		try {
			Query query = entityManager
					.createQuery("select nc from NomCommercial nc where nc.nomCommercial like :searchString or nc.nomChimique like :searchString or nc.fabricant like :searchString");
			query.setParameter("searchString", searchString);
			List<NomCommercial> nomCommercialList = (List<NomCommercial>) query
					.getResultList();
			log.debug("search successful");
			return nomCommercialList;
		} catch (RuntimeException re) {
			log.error("search failed", re);
			return new ArrayList<NomCommercial>();
		}
	}

}
